{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Json
import Control.Monad.IO.Class
import Network.HTTP.Req
import qualified Data.ByteString.Char8 as B
import System.Exit

main :: IO ()
main = runReq defaultHttpConfig $ do
  bs <- req GET (https "loadaverage.org" /: "api" /: "statuses" /: "public_timeline.as") NoReqBody bsResponse mempty
  let r = responseBody bs
  liftIO $ case Json.runParser Json.value $ Input 0 (B.unpack r) of
    Right (_, json) -> do
      liftIO $ putStrLn ("Parsed as: " ++ show json)
    Left (ParserError loc msg) -> do
      putStrLn $
        "[ERROR] Parser failed at character " ++ show loc ++ ": " ++ msg
      liftIO $ exitFailure
  liftIO $ B.putStrLn r
  liftIO $ exitFailure
